package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.trkj.dao.jpa.IHwcSchedulingDAO;
import com.trkj.dao.jpa.IHwcShiftDAO;
import com.trkj.pojos.Tbscheduling;
import com.trkj.pojos.Tbshift;
import com.trkj.vo.DayWeekVO;
/**
 * 排班业务类
 * @author lenovo
 *
 */
@Service
public class HwcSchedulingService {
	
	@Autowired
	private IHwcSchedulingDAO schedulingDAO;
	
	@Autowired
	private IHwcShiftDAO shiftDAO;
	/** 
	 * 根据日期获得所在周的日期  
	 * @param mdate 
	 * @return 
	 */  
	
	
	public List<String> dateToWeek() {  
		//设置格式为年月份周
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd EEE");  
		 //获得系统时间
		Date mdate = new Date();
		
	    int b = mdate.getDay();  
	    Date fdate;  
	    List<Date> list = new ArrayList<Date>();  
	    Long fTime = mdate.getTime() - b * 24 * 3600000;  
	    for (int a = 1; a <= 7; a++) {  
	        fdate = new Date();  
	        fdate.setTime(fTime + (a * 24 * 3600000));  
	        list.add(a-1, fdate);  
	    }  
	    //新建一个集合接受返回来的值
	    List<String> dateList = new ArrayList<String>();
	    for(Date date : list){
	    	
		   	dateList.add(sdf.format(date));
	    }
	   return dateList;
	}  
	
	/**
	 * 将得来的时间通过dayweekVO存起来
	 * @return
	 */
	public List<DayWeekVO> getDayWeek(){
		List<DayWeekVO> dw = new ArrayList<DayWeekVO>();
		List<String> strlist=dateToWeek();
		for(String s:strlist){
			//System.out.println(s);
			
			DayWeekVO dwv = new DayWeekVO();
			
			dwv.setVoWeek(s.substring(11));
		//	System.out.println("输出看看"+s.substring(11)+"长度"+s.substring(11).length());
			dwv.setVoDay(s.substring(0,10));
			//System.out.println("输出看看"+s.substring(0,10)+"长度"+s.substring(0,10).length());
			dw.add(dwv);
		}
		//System.out.println(dw);
		return dw;
		
	}
	
	/**
	 * 添加排版信息
	 * @param tbscheduling
	 */
	public void addScheduling(long staffNo,long shiftNo,long departmentNo,String schedulingDate){
		Tbscheduling scheduling = new Tbscheduling();
		
		scheduling.setSchedulingStaffNo(new BigDecimal(staffNo));
		scheduling.setSchedulingShiftNo(new BigDecimal(shiftNo));
		scheduling.setSchedulingDepartmentNo(new BigDecimal(departmentNo));
		scheduling.setDutyDate(schedulingDate);
		//System.err.println(departmentNo+"-------123");
		schedulingDAO.save(scheduling);
	}
	
	/**
	 * 根据员工编号查询该员工最新的班次名称
	 * @param staffNo
	 * @return
	 */
	public List<String> queryShiftNo(BigDecimal staffNo){
		 List<BigDecimal> shiftNos= schedulingDAO.queryShiftNo(staffNo);
		 List<String> list = new ArrayList<String>();
		 for(BigDecimal shiftNo:shiftNos){
			Tbshift shift= shiftDAO.findOne(shiftNo.longValue());
			if(shift!=null&&shift.getShiftName().length()>0){
				list.add(shift.getShiftName());
			}else{
				list.add("无班次");
			}
		 }
		 return list;
	}
}
